package jzqb.service.impl;


import haha.life.common.CutPageBean;
import haha.life.common.PermissionTypeTool;
import jzqb.bo.MessageBo;
import jzqb.common.Result;
import jzqb.dao.AdminUserInfoDao;
import jzqb.dao.MessageDao;
import jzqb.po.AdminUserPo;
import jzqb.service.AdminDepartmentService;
import jzqb.service.AdminUserInfoService;
import jzqb.service.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 自用库业务层实现类
 */
@Service
public class MessageServiceImpl implements MessageService {

    @Autowired
    MessageDao messageDao;

    @Autowired
    AdminUserInfoService adminUserInfoService;

    @Autowired
    AdminUserInfoDao adminUserInfoDao;

    @Autowired
    AdminDepartmentService adminDepartmentService;


    @Autowired
    MessageService messageService;

    @Override
    public Boolean insertMessage(MessageBo messageBo) {
        String now = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        messageBo.setPublishData(now);
        return messageDao.insertMessage(messageBo);
    }

    @Override
    public Result getMessageList(MessageBo messageBo, String userId) {
        CutPageBean<MessageBo> cutPageBean = new CutPageBean<>();
        messageBo.setStart((messageBo.getPage() - 1) * messageBo.getPageSize());
        List<MessageBo> messageBos = messageDao.getMessageList(messageBo);
        if (messageBos.size() > 0) {
            for (MessageBo bo : messageBos) {
                AdminUserPo adminUserPo = adminUserInfoDao.findUserById(bo.getPublisher());
                //留言人可能已经被清除，不能通过id查询到此人的姓名等信息
                if (adminUserPo != null){
                    bo.setPublisher(bo.getPublisher());
                    bo.setName(adminUserPo.getName());
                    String DeptName = adminDepartmentService.getDeptNameById(adminUserPo.getDepartmentid());
                    bo.setDept(DeptName);
                    //是否显示按钮
                    boolean res = adminUserInfoService.belongToPermissionGroup(userId, PermissionTypeTool.front_businessManageGroupId);
                    bo.setShow(res);
                    //通过id 是什么状态
                    bo.setBan(adminUserPo.getMuteFlag());
                }else{
                    bo.setPublisher(bo.getPublisher());
                    bo.setName("未知");
                    bo.setDept("未知");
                    bo.setShow(adminUserInfoService.belongToPermissionGroup(userId, PermissionTypeTool.front_businessManageGroupId));
                    bo.setBan("1");
                }

            }
        }
        Long count = messageDao.getMessageListCount(messageBo);
        cutPageBean.initCutPage(count.intValue(), messageBo.getPageSize(), messageBos);
        return Result.succ(cutPageBean);
    }

    @Override
    public Boolean deleteMessage(MessageBo messageBo) {
        Boolean result = messageDao.deleteMessage(messageBo);
        return result;
    }
}
